﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bizall.Dynamic.Aspect;

namespace DynamicAspectTester.Mocks
{
    class MyAspect : AspectBase
    {
        public bool HasAround { get; set; }

        public bool BeforeCall { get; private set; }

        public bool AfterCall { get; private set; }

        public bool BeforeGet { get; private set; }

        public bool AfterGet { get; private set; }

        public bool BeforeSet { get; private set; }

        public bool AfterSet { get; private set; }

        public bool ExceptionCall { get; private set; }

        public bool AroundCall { get; private set; }


        public MyAspect()
        {
            HasAround = false;
            Sequence = 1;
        }

        public override void OnBeforeMethodCall(WeavingContext context)
        {
            BeforeCall = true;
        }

        public override void OnAroundMethodCall(WeavingContext context)
        {
            AroundCall = true;
        }

        public override void OnAfterMethodCall(WeavingContext context)
        {
           if (context.ReturnValue != null && context.ReturnValue.GetType() == typeof(string))
                context.ReturnValue = "MyAspect";       
            AfterCall = true;
        }

        public override void OnBeforeGetValue(WeavingContext context)
        {
            BeforeGet = true;
        }

        public override void OnAfterGetValue(WeavingContext context)
        {
 
            AfterGet = true;
        }

        public override bool OnExceptionMethodCall(WeavingContext context, Exception ex)
        {
            if (context.InvokeMemberBinder.Name == "Greeting")
                return false;

            ExceptionCall = true;
            return true;
        }

        public override void OnPropertyChanging(WeavingContext context)
        {
            BeforeSet = true;
        }

        public override void OnPropertyChanged(WeavingContext context)
        {
            AfterSet = true;
        }

        public override bool HasArroundMethod(WeavingContext context)
        {
            return HasAround;
        }
    }
}
